<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-09-05 10:31
 *
 * Version: 2.0
 */

namespace App\Modules\Order\Services;


use App\Exceptions\AllValidatorException;
//use App\Modules\MemberCard\Repositores\MemberCardRepository;
//use App\Modules\Service\Repositories\ServiceRepository;
//use Flower\Core\Repository\Eloquent\BaseService;
//
//class HaircutPriceService extends BaseService
trait HaircutPriceService
{
//	private $serviceRepository;
//	private $cardRepository;
//	public function __construct(ServiceRepository $serviceRepository, MemberCardRepository $memberCardRepository)
//	{
//		$this->serviceRepository = $serviceRepository;
//		$this->cardRepository = $memberCardRepository;
//	}


	/**
	 * 服务价格总价
	 * @param $serviceId
	 * @param int $count
	 * @param int $payment
	 * @param int $memberCardId
	 * @return mixed
	 * @throws AllValidatorException
	 */
//	public function hairCutTotalPrice($serviceId, $count = 1, $payment = 1, $memberCardId = 0,$isFront=true)
	public function hairCutTotalPrice($orderItems,$payment=1,$memberCardId=0,$isFront=true)
	{
		$totalRealPrice = 0;
		$discountMemberCard = 0;
		foreach ($orderItems as $v){
		    if(empty($v['service_id'])) continue;

			// 验证服务是否存在
			$service = $this->serviceRepository->isExisted($v['service_id']);
			$count = isset($v['count']) ? $v['count'] : 1;
			// 服务原价的总价
			$price = $service->price * $count;
			// 真实付款总价
			$totalRealPrice += round($price, 2);
		}
		$serviceIds = array_column($orderItems,'service_id');

		// 计算消费金额
		if (($payment == config('constant.payment.member_card')) && !empty($memberCardId)) {
			$memberCardId = $this->cardRepository->isExisted($memberCardId)->id;
			$cardInfo = $this->cardRepository->isSuitableByServiceId($serviceIds, $memberCardId);
			if($cardInfo->attribute == config('constant.memberCard.cardType.recharge')){
				$cardDiscount = $cardInfo->cardType->cardTypeService[0]->discount;
				$discountRealPrice =  round($price * $cardDiscount * 0.01,2);

			}else if($cardInfo->attribute == config('constant.memberCard.cardType.count')){
				$discountRealPrice = round( $cardInfo->cardType->unit_price * $count,2);
			}

			// 会员卡余额不足
			if(((floatval($cardInfo->balance) - $discountRealPrice ) < 0) && $isFront){
				throw new AllValidatorException(['msg'=>'对不起，您的会员卡余额不足，剩余金额为：'.$cardInfo->balance.'元，请及时充值！']);
			}

			$discountMemberCard = round($totalRealPrice - $discountRealPrice,2);
			if(isset($discountRealPrice) && $discountMemberCard>0){
				$totalRealPrice = $discountRealPrice;
			}
		}

		$resultData['discount_info'] = [
			[
				'name' => '原价',
				'value' => $totalRealPrice
			],
		];
		if($discountMemberCard){
			array_push($resultData['discount_info'],[
				'name'  => '会员卡优惠',
				'value' =>$discountMemberCard
			]);
		}

		$resultData['total_price'] = round($totalRealPrice,2);

		return $resultData;
	}

	public function checkServiceSuitBarberList($orderItems,$storeId){
		return  array_map(function ($v) use($storeId) {
			// 验证助理是否存在
			if(!empty($v['assistant_id'])){
				$v['assistant'] = $this->employeeRepository->isExited($v['assistant_id']);
			}
			// 验证理发师是否存在
			if(!empty($v['barber_id'])){
				$v['barber'] = $this->employeeRepository->isExited($v['barber_id']);
			}
			if(!empty($v["service_id"])){
				$v['service'] =$this->serviceRepository->isExisted($v['service_id']);
				$serviceSuit = $this->employeeRoyaltyRepository->checkBarberServiceIsSuited($v['barber_id'],$v['service_id'],$storeId);
				$v['royalty'] = empty($serviceSuit) ? 0 : $serviceSuit;
			}
			return $v;
		},$orderItems);
	}



}
